using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Umdf.ComInterop;
using System.IO;

namespace Umdf.SideShow.Events
{
    public abstract class SideShowEvent : SideShowObject
    {
        private Guid _id;

        protected SideShowEvent()
        {
            _id = GetEventId(this.GetType());
        }

        public Guid Id
        {
            get { return _id; }
        }

        protected abstract void WriteEventData(Stream s);

        internal void CreateEventData(Stream s)
        {
            WriteEventData(s);
        }

        internal static Guid GetEventId(Type type)
        {
            if (type.IsDefined(typeof(EventIdAttribute), true))
            {
                EventIdAttribute attr = (EventIdAttribute)type.GetCustomAttributes(typeof(EventIdAttribute), true)[0];
                return attr.Value;
            }
            else
            {
                throw new ArgumentException("The type " + type.FullName + " does not contain a EventIdAttribute to identify the SideShow event id.");
            }
        }

        protected internal override void DeserializeObject(BinaryReader reader)
        {
            base.DeserializeObject(reader);
            _id = new Guid(reader.ReadBytes(16));
        }

        protected internal override void SerializeObject(BinaryWriter writer)
        {
            base.SerializeObject(writer);
            writer.Write(_id.ToByteArray());
        }
    }
}
